﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MISAPP.Classify.Service;
using System.Data;

public partial class View_MIS_SquaredQuery : ViewMatrix
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    /// <summary>
    /// 從tempload 資料夾 存到 download 去
    /// </summary>
    protected string templateToDownload(string p_name,string p_ext)
    {        
        return Utility.TemplateToDownload(Server.MapPath("~"),p_name,p_ext);
    }

    protected void GetData()
    {
        GetData(string.Empty);
        //GetData2(); --新方法 2013-04-19 先mark
    }

    protected void GetData(string p_after)
    {
        try
        {
            Service_Squared servSeq = new Service_Squared();
            List<Squared> seqList = servSeq.GetSquared(getResultValue(1));
            Squared_Excel seq_xls = new Squared_Excel(
                servSeq.GetSquaredCaption()
                );

            string dlFile = templateToDownload("Template.xls","xls");
            //Response.Write(DateTime.Now.ToString());
            //Response.Write("<br>");
            seq_xls.GetSquaredResult(dlFile, servSeq, true);
            //seq_xls.GetSquaredResult(dlFile, servSeq);
            //seq_xls.GetSquaredResult(dlFile, servSeq);
            //Response.Write(DateTime.Now.ToString());
            //Response.Write("<br>");
            linkExcel.NavigateUrl = string.Format("~/download/{0}",
                System.IO.Path.GetFileName(dlFile));
            linkExcel.Text = "目前結果";
            seq_xls.ReleaseExcel();
            
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message;
        }
    }

    protected void GetData2()
    {
        //Squared_Excel seq_xls = new Squared_Excel();
        string dlFile = templateToDownload("ForDownLoad.xls", "xls");
        //seq_xls.GetSquaredResult(dlFile, servSeq, true);
        Service_Squared servSeq = new Service_Squared();
        servSeq.FillSquared2Xls(dlFile);
    }

    protected void btnCurrent_Click(object sender, EventArgs e)
    {
        GetData();
    }

    protected void btnAfter7_Click(object sender, EventArgs e)
    {
        GetData("123");
    }

    protected void btnPPT_Click(object sender, EventArgs e)
    {
        Service_Presentation ppt = new Service_Presentation();
        //ppt.MergePresentation(@"D:\VS2010B2\MISApp\WebAPP\UPLoad\CIB_20110110112425_CFG.ppt");
    }

    protected void btnBonus_Click(object sender, EventArgs e)
    {
        get_Service_Bonus_result("BONUS", "Bonus_Space.xls");
        /*
        Service_Bonus bonus = new Service_Bonus();
        Squared_Excel seq_xls = new Squared_Excel();
        string dlFile = templateToDownload("Bonus_Space.xls","xls");
        seq_xls.GetBonusResult(dlFile, bonus);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        seq_xls.ReleaseExcel();
        */
    }

    protected void btnKpi_Click(object sender, EventArgs e)
    {
        Service_KPI kpi = new Service_KPI();
        List<Squared> seqList = kpi.GetKPISheet(getResultValue(1));
        Squared_Excel se = new Squared_Excel();
        string dlFile = templateToDownload("KPI.xls","xls");
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        se.GetKPIAll(kpi, dlFile);
        se.ReleaseExcel();

    }

    protected void btnBus_Click(object sender, EventArgs e)
    {
        presentatoin(Service_Presentation.PPTType.PPT經委會);
    }

    protected void btnBus2_Click(object sender, EventArgs e)
    {
        presentatoin(Service_Presentation.PPTType.PPT經委會預算報告);
    }


    protected void btnPPT2_Click(object sender, EventArgs e)
    {
        presentatoin(Service_Presentation.PPTType.PPT明年度預算);
    }

    protected void presentatoin(Service_Presentation.PPTType p_type)
    {
        Service_Presentation ppt = new Service_Presentation();
        string dlFile = templateToDownload("Presentation.ppt", "ppt");
        ppt.MergePresentation(dlFile, p_type);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        ppt.ReleasePPT();    
    }

    protected void btnQ1_Click_old(object sender, EventArgs e)
    {
        get_Service_Bonus_result("Q1", "BU追蹤表100Q1.xls");
        /*
        //Service_Q1 bonus = new Service_Q1();
        Service_Bonus bonus = new Service_Bonus("Q1");
        Squared_Excel seq_xls = new Squared_Excel();
        string dlFile = templateToDownload("BU追蹤表100Q1.xls", "xls");
        //seq_xls.GetQ1Result(dlFile, bonus);
        seq_xls.GetBonusResult(dlFile, bonus);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        seq_xls.ReleaseExcel();
          */
    }

    protected void btnQ1_Click(object sender, EventArgs e)
    {
        get_bonus_result(Service_Bonus.BonusType.Q1, "BU追蹤表100Q1.xls");
    }

    protected void get_bonus_result(Service_Bonus.BonusType p_type, string p_temp_file)
    {
        Service_Bonus bon = new Service_Bonus();
        string dlFile = templateToDownload(p_temp_file, "xls");
        bon.FillBonus2Xls(dlFile, p_type);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
    }

    protected void btnBudget_Click(object sender, EventArgs e)
    {
        
        //get_Service_Bonus_result("BUDGET", "budget.xls");
        get_bonus_result(Service_Bonus.BonusType.BUDGET, "BUDGET.xls");
    }

    protected void btnCost_Click(object sender, EventArgs e)
    {
        get_Service_Bonus_result("COST", "營業費用BG.xls");
        /*
        Service_Bonus bonus = new Service_Bonus("COST");
        Squared_Excel seq_xls = new Squared_Excel();
        string dlFile = templateToDownload("營業費用BG.xls", "xls");
        //seq_xls.GetCostResult(dlFile, bonus);
        seq_xls.GetBonusResult(dlFile, bonus);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        seq_xls.ReleaseExcel();
         */ 
    }

    protected void get_Service_Bonus_result(string p_team,string p_tempFile)
    {
        //Response.Write(DateTime.Now.ToString());
        //Response.Write("<br>");
        Service_Bonus bonus = new Service_Bonus(p_team);
        Squared_Excel seq_xls = new Squared_Excel();
        string dlFile = templateToDownload(p_tempFile, "xls");
        //seq_xls.GetBudgetResult(dlFile, bonus);
        seq_xls.GetBonusResult(dlFile, bonus);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        seq_xls.ReleaseExcel();
        //Response.Write(DateTime.Now.ToString());
        //Response.Write("<br>");
    }

    protected void btnBudgetPPT_Click(object sender, EventArgs e)
    {
        presentatoin(Service_Presentation.PPTType.PPT預算執行分析附件);
    }
}
